package Week.w150;

import org.junit.Test;

import java.util.*;

public class Solution1 {
    public int countCharacters(String[] words, String chars) {
        if(chars==null||chars.length()==0){
            return 0;
        }
        if(words==null||words.length==0){
            return 0;
        }
        List<Integer> cans=new ArrayList<>();
        for(int i=0;i<words.length;i++){
            if(words[i]==null){
                continue;
            }
            if(canRec(words[i],chars)){
                cans.add(i);
            }
        }

        int result=0;
        for(Integer i:cans){
            result+=words[i].length();
        }
        return result;
    }

    private boolean canRec(String word,String chars){
        if(word.length()>chars.length()){
            return false;
        }
        Map<Character,Integer> map=buildCharMap(chars);
        for(int i=0;i<word.length();i++){
            char c=word.charAt(i);
            if(!map.containsKey(c)){
                return false;
            }
            int value=map.get(c)-1;
            if(value<0){
                return false;
            }
            map.put(c,value);
        }
        return true;
    }

   private Map<Character,Integer> buildCharMap(String chars) {
       Map<Character, Integer> map = new HashMap<>();
       for (int i = 0; i < chars.length(); i++) {
           char charI = chars.charAt(i);
           if (map.containsKey(charI)) {
               map.put(charI, map.get(charI) + 1);
           } else {
               map.put(charI, 1);
           }

       }
       return map;
   }

   @Test
    public void test(){
       String[] words=new String[]{"dyiclysmffuhibgfvapygkorkqllqlvokosagyelotobicwcmebnpznjbirzrzsrtzjxhsfpiwyfhzyonmuabtlwin",
               "ndqeyhhcquplmznwslewjzuyfgklssvkqxmqjpwhrshycmvrb",
               "ulrrbpspyudncdlbkxkrqpivfftrggemkpyjl",
               "boygirdlggnh",
               "xmqohbyqwagkjzpyawsydmdaattthmuvjbzwpyopyafphx",
               "nulvimegcsiwvhwuiyednoxpugfeimnnyeoczuzxgxbqjvegcxeqnjbwnbvowastqhojepisusvsidhqmszbrnynkyop",
               "hiefuovybkpgzygprmndrkyspoiyapdwkxebgsmodhzpx",
               "juldqdzeskpffaoqcyyxiqqowsalqumddcufhouhrskozhlmobiwzxnhdkidr",
               "lnnvsdcrvzfmrvurucrzlfyigcycffpiuoo",
               "oxgaskztzroxuntiwlfyufddl",
               "tfspedteabxatkaypitjfkhkkigdwdkctqbczcugripkgcyfezpuklfqfcsccboarbfbjfrkxp",
               "qnagrpfzlyrouolqquytwnwnsqnmuzphne",
               "eeilfdaookieawrrbvtnqfzcricvhpiv",
               "sisvsjzyrbdsjcwwygdnxcjhzhsxhpceqz",
               "yhouqhjevqxtecomahbwoptzlkyvjexhzcbccusbjjdgcfzlkoqwiwue",
               "hwxxighzvceaplsycajkhynkhzkwkouszwaiuzqcleyflqrxgjsvlegvupzqijbornbfwpefhxekgpuvgiyeudhncv",
               "cpwcjwgbcquirnsazumgjjcltitmeyfaudbnbqhflvecjsupjmgwfbjo",
               "teyygdmmyadppuopvqdodaczob",
               "qaeowuwqsqffvibrtxnjnzvzuuonrkwpysyxvkijemmpdmtnqxwekbpfzs"
               ,"qqxpxpmemkldghbmbyxpkwgkaykaerhmwwjonrhcsubchs"};
      String chars="usdruypficfbpfbivlrhutcgvyjenlxzeovdyjtgvvfdjzcmikjraspdfp";
       System.out.println(countCharacters(words,chars));
   }
}
